﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    private SpriteRenderer _sr;
    private Vector3 _bulletEulerAngle;
    private float _fireTimer = 0;
    public float fireCD = 0.5f;
    public Sprite[] TankSprites;//0上、1右、2下、3左
    public float moveSpeed = 5;
    public GameObject bullet;

    private bool _isProtected;//无敌
    private float _protectedTimer;//无敌计时器
    public int Life { get; private set; }

    private void Awake()
    {
        _sr = gameObject.GetComponent<SpriteRenderer>();
        Life = 1;
        _isProtected = true;
    }

    void Start()
    {


    }

    void Update()
    {
        if (_isProtected)
        {
            Transform shield = transform.Find("Shield");
            shield.gameObject.SetActive(true);
            _protectedTimer += Time.deltaTime;
            if (_protectedTimer > 3)
            {
                _isProtected = false;
                shield.gameObject.SetActive(false);
            }
        }
        if (_fireTimer > fireCD)
        {
            Attack();
        }
        else
        {
            _fireTimer += Time.deltaTime;
        }
    }

    private void FixedUpdate()
    {
        Move();
    }

    //攻击
    private void Attack()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject playerBullet = Instantiate(bullet, transform.position, Quaternion.Euler(_bulletEulerAngle));
            playerBullet.tag = "MyTank";
            _fireTimer = 0;
        }
    }

    //玩家移动
    private void Move()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        if (h != 0)
        {
            transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
            if (h > 0)
            {
                _sr.sprite = TankSprites[1];
                _bulletEulerAngle = new Vector3(0, 0, -90);
            }
            else
            {
                _sr.sprite = TankSprites[3];
                _bulletEulerAngle = new Vector3(0, 0, 90);
            }

        }
        else if (v != 0)
        {
            transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
            if (v > 0)
            {
                _sr.sprite = TankSprites[0];
                _bulletEulerAngle = new Vector3(0, 0, 0);
            }
            else
            {
                _sr.sprite = TankSprites[2];
                _bulletEulerAngle = new Vector3(0, 0, -180);
            }
        }
    }

    private void Hurt()
    {
        if (_isProtected)
        {
            return;
        }
        Life--;
        if (Life <= 0)
        {
            Die();
        }
    }

    public void Die()
    {
        //GameObject ExplosionPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Effect/Explosion.prefab", typeof(GameObject)) as GameObject;
        //GameObject explossionEffict= Instantiate(ExplosionPrefab,transform.position,Quaternion.identity);
        //Destroy(explossionEffict, 0.2f);
        EffectManager.Instance.PlayEffect("Explosion", transform.position, Quaternion.identity, 0.3f);
        Destroy(gameObject);
    }

}
